import {
    cancelOrDeleteOrder
} from '../../../api/index'

export default {
    props: {
        handleIsDetails: Function,
        orderDetail: Object
    },
    data() {
        return {
            src: require('../../../assets/test02.jpg'),
            BaseUrl: process.env.VUE_APP_WAREHOUSE_API,
            countDown: {
                minute: '00',
                second: '00'
            },
            setIntervalID: null, // 定时器ID
            currentTime:''
        }
    },
    methods: {

        // 取消订单
        handleDeleteOnOrder() {
            this.$confirm('确定取消“' + this.orderDetail.orderNo + '”这个订单吗？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let DeleteNumbers = [this.orderDetail.orderNo]
                this.handleCancelOrDeleteOrder(DeleteNumbers)
            }).catch(() => {

            })
        },

        // 取消订单请求
        handleCancelOrDeleteOrder(deNumberArr) {
            const loading = this.$loading({
                lock: true,
                text: '正在取消...',
                spinner: 'el-icon-loading',
                background: 'rgba(0, 0, 0, 0.7)'
            })
            cancelOrDeleteOrder(deNumberArr).then(res => {
                if (res.data.code === 200) {
                    this.$message({
                        message: '取消成功',
                        type: 'success'
                    })
                    clearInterval(this.setIntervalID)
                    this.countDown = {
                        minute: '00',
                        second: '00'
                    }
                    this.orderDetail.status = 3
                    this.getCurrentTime()
                } else {
                    this.$message({
                        message: res.data.msg,
                        type: 'warning'
                    })
                }
                loading.close()
            }).catch(err => {
                loading.close()
                // this.$message.error('取消或删除订单失败')
            })
        },

        // 付款|| 再次买
        handleContinueBuy() {
            this.$router.push({
                path: '/order',
                query: {
                    orderNumber: this.orderDetail.orderNo
                }
            })
        },

        // 根据下单时间返回倒计时时间
        handleCountDown(val) {
            let nowTime = new Date()

            let createdTime = new Date(val)

            let TIME = 1000 * 60 * 30

            // 目标时间和当前时间的毫秒数

            let differ = nowTime - createdTime

            // 目标时间超过当前时间，或目标时间与当前时间的差超过30分钟则返回0

            if (differ < 0 || differ > TIME) {

                return 0

            }


            // 剩余时间的秒数

            differ = TIME - differ

            // 计算分钟数

            let minute = Math.floor(differ / (60 * 1000))

            minute = minute < 10 ? '0' + minute : minute

            // 计算秒数

            let second = Math.floor((differ - minute * (60 * 1000)) / 1000)

            second = second < 10 ? '0' + second : second

            // 返回结果格式 29:59

            return {
                minute,
                second
            }
        },

        // 定时器
        handleSetInterVal() {
            if (this.orderDetail.status === 1) {
                this.setIntervalID = setInterval(() => {
                    let {minute, second} = this.handleCountDown(this.orderDetail.createTime)
                    if (minute >= 0 && second >= 0) {
                        this.countDown = {
                            minute,
                            second
                        }
                    } else {
                        clearInterval(this.setIntervalID)
                        let DeleteNumbers = [this.orderDetail.orderNo]
                        cancelOrDeleteOrder(DeleteNumbers).then(res => {
                            if (res.data.code === 200) {
                                this.orderDetail.status = 3
                                this.getCurrentTime()
                            } else {
                                console.log('倒计时结束取消订单失败')
                            }
                        }).catch(err => {
                            console.log('倒计时结束取消订单失败',err)
                            // this.$message.error('取消或删除订单失败')
                        })
                    }
                }, 1000)
            }
        },

        // 获取当前时间
        getCurrentTime(){
            let date = new Date()
            this.currentTime = this.$moment(date).format('YYYY-MM-DD HH:mm:ss')
        }
    },
    created() {
        this.handleSetInterVal()
    },

    destroyed() {
        clearInterval(this.setIntervalID)
    }
}
